<template>
  <div>
    <el-form :inline="true" :model="data.selectData" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="data.selectData.nickName" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="data.selectData.role" placeholder="请选择" clearable>
          <el-option  label="全部" :value="0" />
          <el-option v-for="item in data.roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="data.list" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="180" />
      <el-table-column prop="nickName" label="姓名" width="180" />
      <el-table-column prop="role" label="角色">
        <template #default="scope">
          <el-button v-for="item in scope.row.role" :key="item.role" size="small" link>
            {{item.roleName}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="role" label="操作">
        <template #default="scope">
          <el-button type="primary" size="small" @click="changeUser(scope.row)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <el-dialog v-model="data.isShow" title="编辑信息">
    <el-form :model="data.active">
      <el-form-item label="姓名" label-width="50px">
        <el-input v-model="data.active.nickName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="角色" label-width="50px">
        <el-select multiple v-model="data.active.role" placeholder="请选择" >
          <el-option v-for="item in data.roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.isShow = false">取消</el-button>
        <el-button type="primary" @click="updateUser">
          更改
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, reactive, watch } from 'vue'
import { getUserList, getRoleList } from '../request/api'
import { InitData, type ListInt } from '../type/user'

const data = reactive(new InitData())

onMounted(() => {
  getUser()
  getRole()
})

const getUser = () => {
  getUserList()
    .then((res) => {
      console.log(res)
      data.list = res.data
    })
    .catch(() => { })
}

const getRole = () => {
  getRoleList()
    .then((res) => {
      console.log(res)
      data.roleList = res.data
    })
    .catch(() => { })
}

const onSubmit = () => {
  let arr: ListInt[] = [] // 定义数组，用来接受查询过后的数据
  if (data.selectData.nickName || data.selectData.role) {
    // 判断是否其中一个有值
    if (data.selectData.nickName) {
      arr = data.list.filter((value) => {
        return value.nickName.indexOf(data.selectData.nickName) !== -1
      })
    }
    if (data.selectData.role) {
      arr = (data.selectData.nickName ? arr : data.list).filter((value) => {
        return value.role.find((item) => item.role === data.selectData.role)
      })
    }
  } else {
    arr = data.list
  }
  data.list = arr
}

watch([() => data.selectData.nickName, () => data.selectData.role], () => {
  if (data.selectData.nickName == '' && data.selectData.role == 0) {
    getUser()
  }
})

const changeUser = (row: ListInt) => {
  data.isShow = true
  data.active = {
    id: row.id,
    nickName: row.nickName,
    userName: row.userName,
    role: row.role.map((item: any) => item.role || item.roleId)
  }
}

const updateUser = () => {
  let obj: any = data.list.find((value) => value.id === data.active.id)
  obj.nickName = data.active.nickName
  obj.role = data.roleList.filter((value) => data.active.role.indexOf(value.roleId) !== -1)
  console.log(obj);

  data.list.forEach((item,i) => {
    if (item.id === obj.id) {
      data.list[i] = obj
    }
  })
  data.isShow = false
}


</script>

<style lang="scss" scoped></style>
